package me.heshang.rednamedrop;
import me.heshang.rednamedrop.Connection.MySql;
import org.bukkit.World;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChangedWorldEvent;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;


/**
 * 红名村
 */
public class RedWorld extends Thread implements Listener  {
    private static String RedWorld;
    private static String PCWE;
    private static int Time;
    public static String PL;
    public static Player player;
    static PreparedStatement ps = null;
    public class XXX extends Thread {
        private int curSec;
        @Override
        public void run() {

            while(PCWE.equals(RedWorld)){
            if (PCWE.equals(RedWorld)) {


                this.curSec = Time;
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    public void run() {

                    }
                }, 0, 1000);
                try {
                    timer.cancel();
                    TimeUnit.SECONDS.sleep(Time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                timer.cancel();
                try {
                    if (!PCWE.equals(RedWorld)) {
                        //player.getPlayer().sendMessage("§b|> §e你已离开忏悔世界");
                        interrupt();

                    }else {
                        new MySql().querylever();
                        if (new MySql().getLEVER() < 1){
                            player.getPlayer().sendMessage(new RedNamedrop().getMessage_redclear());
                        }else{
                            new MySql().reduce();
                            player.getPlayer().sendMessage(new RedNamedrop().getMessage_redreduce());
                            int i = new MySql().getLEVER()-1;
                            player.getPlayer().sendMessage(new RedNamedrop().getMessage_redlevel()+i);
                        }

                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

            }
            }
        }
    }

    @EventHandler
    public void RedWorld(PlayerChangedWorldEvent p) throws InterruptedException, SQLException, ClassNotFoundException {
        XXX thread = new XXX();
        new MySql().querylever();
        player = p.getPlayer();
        PL = player.getName();
        PCWE =  p.getPlayer().getWorld().getName();
        if (PCWE.equals(RedWorld)) {
            player.getPlayer().sendMessage(new RedNamedrop().getMessage_redworld());
            thread.start();
        }/**else { //去除原因:只要前往是不是忏悔世界则会输出
            player.getPlayer().sendMessage("§b|> §e你已离开忏悔世界");
        }
         **/


    }
    public void thread() throws InterruptedException {
        XXX thread = new XXX();
        thread.interrupt();
        thread.join();
    }



    public static String getRedWorld(String redWorld) {
        return RedWorld;
    }

    public static void setRedWorld(String redWorld) {
        RedWorld = redWorld;
    }

    public static int getTime(int anInt) {
        return Time;
    }

    public static void setTime(int time) {
        Time = time;
    }

    public static String getPL() {
        return PL;
    }


}
